package com.exam.scjp5.objectives.section_1;

public class ExampleConstructor
{
    public static void main(String args[])
    {
        new ChildConstructor(10);
    }
}

/**
 * If your class does not have Constructor, the compiler will create one automatically.
 * ParentConstructor class has 2 constructors. So, in this case, the compiler will not
 * create default constructor (no arg constructor) for you.
 */
class ParentConstructor
{
    private int i;
    
    // This parent class does not have DEFAULT constructor.
    // So, its child classes must explicitly implement their own constructor(s).
    
    public ParentConstructor(int i)
    {
        this.i = i;  
        System.out.println("ParentConstructor(int i);");
    }
    
    public ParentConstructor(int i1, int i2)
    {
        this(i1 * i2);
        
        System.out.println("ParentConstructor(int i1, int i2);");
    }
}

/**
 * This child class extends ParentConstructor. 
 * The child class must explicitly call parent class's construtor.
 * Constructors are not inherited.
 */
class ChildConstructor extends ParentConstructor
{
    // If you do not implement any constructor here, the compiler will show errors
    // because parent class has no DEFAULT constructor.
    
    public ChildConstructor()
    {
        // If you do not explicitly call one of the parent's constructors,
        // the compiler will show ERROR. You can call any constructor of your parent's.
        super(10); 
        // If you want to call 'super(10,10)' instead of 'super(10)',
        // that's also OK.
        // e.g.->  super(10,10); 
    }
    
    public ChildConstructor(int i1)
    {
        super(i1);
        System.out.println("ChildConstructor(int i1);");
    }
    
}

/**
 *  This is another parent class but which has a default constructor. 
 */

class ParentDefaultConstructor
{
    public ParentDefaultConstructor() // DEFAULT constructor !!!
    { }
    
    public ParentDefaultConstructor(int i)
    { }
}

class ChildDefaultConstructor extends ParentDefaultConstructor
{
    // This child class no need to have any constructors,
    // because its parent class has default constructor.
    // But if you want, you can add one.    
    public ChildDefaultConstructor(int i,int j)
    {
        // No need to explicitly call parent's constructor  
        // because the parent has default constructor.
        // Compiler will implicitly call parent's default constructor.
    }
}
